package com.xpsoft.oa.workflow.event;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.project.ProjectWinbid;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.model.system.FileAttach;
import com.xpsoft.oa.service.flow.ProcessRunService;
import com.xpsoft.oa.service.project.ProjectRegisterService;
import com.xpsoft.oa.service.project.ProjectWinbidService;
import com.xpsoft.oa.service.system.AppUserService;
import com.xpsoft.oa.service.system.FileAttachService;

public class ProjectWinBidFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(ProjectWinBidFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String proName = (String)pi.getVariable("projectWinbid.proName");
		String managerUserId = (String)pi.getVariable("projectWinbid.manager.userId");
		String constructionUnit = (String)pi.getVariable("projectWinbid.constructionUnit");
		String proRegisterTitle = (String)pi.getVariable("projectWinbid.proRegisterTitle");
		String proRegisterId = (String)pi.getVariable("projectWinbid.projectRegister.id");
		BigDecimal winBidAmount = (BigDecimal)pi.getVariable("projectWinbid.winBidAmount");
		Date contractWorkLimit = (Date)pi.getVariable("projectWinbid.contractWorkLimit");
		Date contractWorkLimitEnd = (Date)pi.getVariable("projectWinbid.contractWorkLimitEnd");
		String attachIds = (String)pi.getVariable("projectWinbid.attachIds");
		
		ProjectWinbid projectWinbid = new ProjectWinbid();
		projectWinbid.setProName(proName);
		projectWinbid.setConstructionUnit(constructionUnit);
		projectWinbid.setProRegisterTitle(proRegisterTitle);
		projectWinbid.setWinBidAmount(winBidAmount);
		projectWinbid.setContractWorkLimit(contractWorkLimit);
		projectWinbid.setContractWorkLimitEnd(contractWorkLimitEnd);
		
		if(StringUtils.isNotBlank(proRegisterId)){
			ProjectRegisterService projectRegisterService = (ProjectRegisterService)AppUtil.getBean("projectRegisterService");
			projectWinbid.setProjectRegister(projectRegisterService.get(Long.valueOf(proRegisterId)));
		}
		
		if(StringUtils.isNotBlank(managerUserId)){
			AppUserService appUserService = (AppUserService)AppUtil.getBean("appUserService");
			projectWinbid.setManager(appUserService.get(Long.valueOf(managerUserId)));
		}
		
		FileAttachService fileAttachService = (FileAttachService)AppUtil.getBean("fileAttachService");
		if(StringUtils.isNotBlank(attachIds)){
			Set<FileAttach> fileSet = new HashSet<FileAttach>();
			String fileIds[] = attachIds.split(",");
			projectWinbid.getFileAttachs().clear();
			for(String fileId:fileIds){
				fileSet.add(fileAttachService.get(Long.valueOf(fileId)));
			}
			projectWinbid.getFileAttachs().addAll(fileSet);
		}
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		projectWinbid.setProcessRunId(processRun.getRunId());
		
		ProjectWinbidService projectWinbidService = (ProjectWinbidService)AppUtil.getBean("projectWinbidService");
		projectWinbidService.save(projectWinbid);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
